﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace TARADesktopPatient
{
    /// <summary>
    /// Interaction logic for TreatmentItem.xaml
    /// </summary>
    public partial class TreatmentItem : UserControl
    {
        tara_wcf.ItaraServiceClient svc = new tara_wcf.ItaraServiceClient();
        private tara_wcf.EXE treatment = new tara_wcf.EXE();

        public TreatmentItem(tara_wcf.EXE exe)
        {
            this.InitializeComponent();
            treatment = exe;
            lblName.Text = treatment.Name;
            lblDescription.Text = treatment.Description;
            if (exe.ImageByte != null)
            {
                imgTreatmentPic.Source = LoadImage(treatment.ImageByte);
            }
            svc.getLatestUIDCompleted += new EventHandler<tara_wcf.getLatestUIDCompletedEventArgs>(svc_getLatestUIDCompleted);
           
        }

        private void btnRunTreatment_Click(object sender, RoutedEventArgs e)
        {
            svc.getLatestUIDAsync(treatment.Uid, MainPage.patientID);
        }

        void svc_getLatestUIDCompleted(object sender, tara_wcf.getLatestUIDCompletedEventArgs e)
        {
            List<KeyValuePair<string, string>> listTemp = e.Result;
            writeSettings(listTemp);
            try
            {
                startOSK(treatment.Path);
            }
            catch (Exception)
            {
                //closeOSK();
            }
        }

        public void writeSettings(List<KeyValuePair<string, string>> text)
        {
            // create a writer and open the file
            TextWriter tw = new StreamWriter("C:\\myfile.txt");
            string result = "patientID;" + MainPage.patientID + ";";
            foreach (KeyValuePair<string, string> temp in text)
            {
                result += temp.Key + ";";
                result += temp.Value + ";";
            }

            // write a line of text to the file
            tw.WriteLine(result);

            // close the stream
            tw.Close();
        }

        BitmapSource LoadImage(Byte[] imageData)
        {
            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var decoder = BitmapDecoder.Create(ms,
                    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }

        System.Diagnostics.Process process;
        //static 
        bool started = false;

        public void startOSK(string path)
        {
            try
            {
                process = new System.Diagnostics.Process();
                started = true;
                string startupPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); 
                //process.StartInfo = new System.Diagnostics.ProcessStartInfo((Directory.GetCurrentDirectory().Replace("\\bin\\Debug", "") + @"\osk.exe"));
                //process.StartInfo = new System.Diagnostics.ProcessStartInfo("C:\\Windows\\system32\\notepad.exe");
                //process.StartInfo = new System.Diagnostics.ProcessStartInfo("C:\\game3.exe");
                string programPath = startupPath + "\\" + path ;
                //System.Diagnostics.Process.Start(programPath);
                process.StartInfo = new System.Diagnostics.ProcessStartInfo(programPath);
                process.Start();
            }
            catch (Exception)
            {
                MessageBox.Show("Error loading game. Please ensure you have the game in the folder. If problem persist, kindly refer to the user guide.");
            }
        }

        public void closeOSK()
        {
            if (started == true)
            {
                try
                {
                    process.Kill();
                    process.Dispose();
                    process = null;
                    started = false;
                }
                catch (Exception)
                {
                    started = false;
                }
            }
        }
    }
}